# Maintainer: David Runge <dvzrv@archlinux.org>
# Maintainer: Daniel M. Capella <polyzen@archlinux.org>

pkgname=khard
pkgver=0.19.1
pkgrel=1
pkgdesc='Console address book manager'
arch=('any')
url=https://github.com/lucc/khard
license=(GPL-3.0-only)
depends=(
  'python'
  'python-atomicwrites'
  'python-configobj'
  'python-ruamel-yaml'
  'python-unidecode'
  'python-vobject'
)
makedepends=(
  'git'
  'python-build'
  'python-installer'
  'python-setuptools-scm'
  'python-sphinx'
  'python-sphinx-autoapi'
  'python-sphinx-autodoc-typehints'
  'python-wheel'
)
checkdepends=('python-pytest')
optdepends=(
  'diffutils: Using sdiff_khard_wrapper.sh'
  'vdirsyncer: Synchronization of address books with a DAV server'
)
source=($pkgname-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz)
b2sums=('9a3783235b910a543402ba47f98f23a59febdee72985740e0afefa0f27833405f0a0415022848cef495e2718676208c1be093b92fea436f567c4d5cf55be4472')

build() {
  cd $pkgname-$pkgver
  SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver python -m build --wheel --skip-dependency-check --no-isolation
  make -C doc man
}

check() {
  cd $pkgname-$pkgver
  pytest -v
}

package() {
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")

  cd $pkgname-$pkgver
  python -m installer --destdir="$pkgdir" dist/*.whl
  # additional wrapper script
  install -vD misc/sdiff/sdiff_${pkgname}_wrapper.sh \
    "$pkgdir"/usr/lib/$pkgname/sdiff_${pkgname}_wrapper.sh
  # twinkle integration
  install -vDm 644 misc/twinkle/scripts/*.py \
    -t "$pkgdir"/usr/share/$pkgname/twinkle/scripts/
  install -vDm 644 misc/twinkle/sounds/*.ogg \
    -t "$pkgdir"/usr/share/$pkgname/twinkle/sounds/
  # zsh
  install -vDm 644 misc/zsh/_*$pkgname \
    -t "$pkgdir"/usr/share/zsh/site-functions/
  # docs
  install -vDm 644 {CHANGES,CONTRIBUTING.rst,README.md} \
    -t "$pkgdir"/usr/share/doc/$pkgname/
  # man
  install -vDm 644 doc/build/man/$pkgname.1 \
    -t "${pkgdir}/usr/share/man/man1"
  install -vDm 644 doc/build/man/$pkgname.conf.5 \
    -t "${pkgdir}/usr/share/man/man5"
  install -vDm 644 $pkgname/data/{config.spec,template.yaml} -t "$pkgdir/$site_packages/$pkgname/data/"
}
